#ifndef __reg_file_h__
#define __reg_file_h__

#include "string.h"
#include "arm.h"
#include "types.h"
#include "log.h"

class CRegisterFile
{
public:

	ARM_MODE mode;
	__u32 prf[MAX_REG];// physical register file

	CRegisterFile() {}

	void reset()
	{
		memset(&prf,0,sizeof(prf));
		mode=MODE_SVC;
		prf[PC]=0xbf080000;   //XIP
	}


	__u32 read_register(int reg_no)
	{
		return (reg_no==PC)?prf[PC]+8:prf[PREG(reg_no,mode)];
	}

	void write_register(int reg_no,__u32 data)
	{

		ARM_PREG n=PREG(reg_no,mode);

		prf[n]=data;

		//if(reg_no==13&&mode==MODE_FIQ)
		//{
		//	log("R13_IRQ:%x %x %x\n",reg_no,n,data);
		//}
	}

	void set_cpsr(__u32 v)
	{
		prf[CPSR]=v;
		v&=0x1f;
		mode=(v==MODE_USER_VAL)?MODE_USER:
		(v==MODE_IRQ_VAL)?MODE_IRQ:
		(v==MODE_SVC_VAL)?MODE_SVC:
		(v==MODE_FIQ_VAL)?MODE_FIQ:
		(v==MODE_ABORT_VAL)?MODE_ABORT:
		(v==MODE_UNDEFINED_VAL)?MODE_UNDEFINED:
		(v==MODE_SYSTEM_VAL)?MODE_SYSTEM:(ARM_MODE)BUG(BAD_MODE,__LINE__,__FILE__);
	}

	void dump_arm_base_state(char* buffer);
	void dump_arm_bank_state(char* buffer);
	void dump_arm_spsr_state(char* buffer);
	void dump_thumb_base_state(char* buffer);
};
#endif //__reg_file_h__